<?php

/**
 * favori actions.
 *
 * @package    explications_informatiques
 * @subpackage favori
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class favoriActions extends sfActions
{

  
  
  public function executeNew(sfWebRequest $request)
  {
	if($this->getUser()->isAuthenticated())
	{
		$q = Doctrine_Query::create()
			->from('favoris')
			->where('id_client = ?', $this->getUser()->getGuardUser()->getId())
			->andWhere('id_asso = ?', $request->getParameter('idasso'));
		if ($q->count() == 0)
		{
			$favori = new Favoris();
			$favori->setIdClient($this->getUser()->getGuardUser()->getId());
			$favori->setIdArticle($request->getParameter('id'));
			$favori->setIdAsso($request->getParameter('idasso'));
			$favori->save();
			$this->getUser()->setFlash("info","Favori ajouté!");
		}
		else	
			$this->getUser()->setFlash("err","Cet article est déjà dans vos favoris");
	}
	else	
		$this->getUser()->setFlash("err","Vous devez avoir un compte pour ajouter des favoris");

	$this->redirect('@show_article?title=article&idasso='.$request->getParameter('idasso'));
  }

  public function executeDelete(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->isAuthenticated() && $this->getUser()->hasCredential("AJOUT_FAVORIS"),"headers","forbidden");
    $q = Doctrine_Query::create()
		->from('assoplatform n')
		->where('n.idassoplatform = ?',$request->getParameter('idasso'))
		->andWhere("n.id_client = ?", $this->getUser()->getGuardUser()->getId());
	$q->delete();
	$this->getUser()->setFlash("info","Favori supprimé!");
  }

  
  
}
